// *******************************************************************************************************
// # リスト検索画面
//   ・フッター検索ボタン
//   ・選択ボタン
// *******************************************************************************************************

var PageMove;
var ReSearch;

(function(win, doc) {

	/* イニシャライズ
	----------------------------------- */
	function init() {
		var $searchBtn = $('#fixedSearchBtn'),
			$copyright = $('#siteCopyright');

		$searchBtn.show();
		$copyright.css('margin-bottom', 70);
	}

	/* フッター検索ボタン
	----------------------------------- */
	function checkSelectBtn() {
		var $modalBtn = $('.mod_box_area'),
			$selectOne = $('.sec_detail_otherroom .box_expand_inner input'),
			isCheck = false;

		$selectOne.each(function(i){
			if ($(this).prop('checked')) {
				isCheck = true;
				return false;
			};
		});
		if (isCheck) {
			$modalBtn.removeClass('off');
			$modalBtn.find('a:first-child').removeClass('disabled');
			$modalBtn.find('a:first-child').attr('href', '#studylist');			
			
		} else {
			$modalBtn.addClass('off');
			$modalBtn.find('a:first-child').addClass('disabled');
			$modalBtn.find('a:first-child').attr('href', 'javascript: void(0);');
		};
	}

	/* 選択ボタン
	----------------------------------- */
	function setSelectBtn() {
		var $selectAll = $('.btn_group_wrap .select_all'),
			$selectOne = $('.sec_detail_otherroom .box_expand_inner');

		$selectAll.find('input').on('click', function(e){
			if ($(this).prop('checked')) {
				$selectAll.find('input').prop('checked', true);
				$selectOne.find('input').prop('checked', true);
				$selectOne.addClass('active');
			} else {
				$selectAll.find('input').prop('checked', false);
				$selectOne.find('input').prop('checked', false);
				$selectOne.removeClass('active');
			};
			checkSelectBtn();
		});
		$selectOne.each(function(i){
			var $self = $(this);
			$(this).find('input').on('click', function(e){
				var b = $(this).prop('checked');
				if (b) {
					$self.addClass('active');
				} else {
					$self.removeClass('active');
				};
				$selectAll.find('input').prop('checked', false);
				checkSelectBtn();
			});
			if ($(this).find('input').prop('checked')) {
				$self.addClass('active');
			};
		});
	}
	/* モーダル 位置
	----------------------------------- */
	function modalPosition() {
		var timer = null;
		$('a.modal_link').on('click', function(e){			
			var _id = $(this).attr('href'),
				_st = $(window).scrollTop(),
				_wh = $(window).height(),
				_ch = $(_id).height();
			if (_wh > _ch) {
				$(_id).css({ 'top': _st + (_wh-_ch)*0.5 });
			} else {
				$(_id).css({ 'top': _st + 20 });
			};
			clearTimeout(timer);
			timer = setTimeout(function(e){
				$('#lean_overlay').trigger('click');
			}, 1500);
			
			//検討リスク追加
			$('.active').find('input').each(function(){
				KENTO.addKentoList($(this).val());
			});
			
			return false;
		});
	};		
	
	//検索条件保存
	$('.btn_save').on('click', function(){
		
		var items = localStorage.getItem('TEMPCONS');
		if(items != null){
			var itemsName = localStorage.getItem('TEMPCONSNAME');
		}
		CONS.addCondition(JSON.parse(items), JSON.parse(itemsName));
		//console.dir(JSON.parse(items));
		//console.dir(JSON.parse(itemsName));
		
	});
	
	//クッキー用の保存
	function saveInquiryCondition(href){
		
		var items=[];
		var keyHash = {};
		var codes = [];
		
		$('input[type=hidden][name^=this_params]').each(function(){
			var name = $(this).attr('name');
			var val = $(this).val();
			name = name.replace('this_params[', '').replace(']', '');
			keyHash[name] = val;
		});
		
		//console.dir(keyHash);
		
		var square = ['0','1','15','20','25','30','35','40','45','50','60','70','80','90','100','99999'];
		var eki = ['0','1','5','7','10','15'];
		var madoris = ['10', '11', '12', '14', '21', '22', '24', '31', '32', '34', '41', '42', '44', '51'];
		var builts = ['0', '1', '3', '5', '10'];
		var ko1 = [136,20,21,22,23,24];
		var ko2 = [94,116,117,118,119,120,121,122,125,126];
		var ko3 = [9,10,11,12,14,15,18,19,132,134,133,135];
		var ko4 = [31,32,36,93,95,130,131];
		var ko5 = [53,54,55,56,97,98,103,104,111];
		var ko6 = [33,34,75,107,123,124];
		var ko7 = [4,5,8,46,61,62,66,73,77,87,101,112,127,128,129];
		var ko8 = [59,60,67,78,79,80,114];
		var ko9 = [44,45,63,65,81,83,86,113];
		var ko10 = [68,70,90,91,115];
		var ko11 = [7,38,39];
		
		var nameList = JSON.parse(localStorage.getItem('TEMPCONSNAME'));		
		var mainData = nameList[0].mainData;
		//console.dir(mainData);
		
		//name
		var search_type = keyHash['search_type'];
		if(search_type == 'shop') items.push({name:'name',value:mainData[0]});
		else if(search_type == 'area'){
			var names = [];
			for(var i = 0 ; i < mainData.length ; i++){
				var ret = mainData[i].replace('　', ' ');
				var vals = ret.split(' ');
				
				if(vals.length <= 2) names.push(ret.replace(' ', '　'));
				else names.push(vals[0] + '　' + vals[1] + ';' + vals[2]);
			}
			items.push({name:'name',value:names.join('/')});
		}		
		else if(search_type == 'line'){
			var names = [];
			for(var i = 0 ; i < mainData.length ; i++){
				var ret = mainData[i].replace('　', ' ');
				var vals = ret.split(' ');
				
				if(vals.length <= 2) names.push(ret.replace(' ', '　'));
				else names.push(vals[0] + '　' + vals[1] + ';' + vals[2]);
			}
			items.push({name:'name',value:names.join('/')});
		}
		
		//コード
		for(var name in keyHash){
			var value = keyHash[name];
			if(name == 'search_type'){
				if(value == 'area') items.push({name:'kind', value:'1'});
				else if(value == 'line') items.push({name:'kind', value:'2'});
				else if(value == 'shop') items.push({name:'kind', value:'3'});
			}
			else if(name.indexOf('target') >= 0){ //コード
				codes.push(value.split('-')[1]);
			}
			else if(name.indexOf('tinryo') >= 0 && name != 'tinryo_jouken2'){ //賃料
				if(value == 9999999) value = 9990000;
				items.push({name:name, value:value/10000});
			}	
			else if(name.indexOf('menseki') >= 0 || name.indexOf('senyu') >= 0){ //面積
				var index = square.indexOf(keyHash[name]);
				if(name.indexOf('senyu') >= 0){
					if(name == 'senyu1'){
						items.push({name:'senyu1', value: index});
					}
					else if(name == 'senyu2'){
						items.push({name:'senyu2', value: index});
					}
				}
				else {
					if(name == 'menseki1'){
						items.push({name:'menseki1', value: index});
					}
					else if(name == 'menseki2'){
						items.push({name:'menseki2', value: index});
					}
				}
			}			
		}	
		
		items.push({name:'code', value:codes.join(',')});
		
		if(keyHash['tinryo_jouken2'] == 'on') items.push({name:'tinryo_jouken2', value: '1'});
		else items.push({name:'tinryo_jouken2', value: '0'});		
		
		if(keyHash['tyusyajyo_komi'] == 'on') items.push({name:'tyusyajyo_komi', value: '1'});
		else items.push({name:'tyusyajyo_komi', value: '0'});
		
		
		if(keyHash['shikikin_nashi'] == 'on') items.push({name:'shikikin_nashi', value: '1'});
		else items.push({name:'shikikin_nashi', value: '0'});
		
		if(keyHash['reikin_nashi'] == 'on') items.push({name:'reikin_nashi', value: '1'});
		else items.push({name:'reikin_nashi', value: '0'});
		
		if(keyHash['freerent'] == 'on') items.push({name:'freerent', value: '1'});
		else items.push({name:'freerent', value: '0'});
		
		if(keyHash['heyawari'] == 'on') items.push({name:'heyawari', value: '1'});
		else items.push({name:'heyawari', value: '0'});
		
		if(keyHash['syubetu1'] == 'on') items.push({name:'syubetu1', value: '1'});
		else items.push({name:'syubetu1', value: '0'});
		
		if(keyHash['syubetu2'] == 'on') items.push({name:'syubetu2', value: '1'});
		else items.push({name:'syubetu2', value: '0'});
		
		if(keyHash['syubetu3'] == 'on') items.push({name:'syubetu3', value: '1'});
		else items.push({name:'syubetu3', value: '0'});
		
		if(keyHash['syubetu4'] == 'on') items.push({name:'syubetu4', value: '1'});
		else items.push({name:'syubetu4', value: '0'});
		
		if(keyHash['syubetu5'] == 'on') items.push({name:'syubetu5', value: '1'});
		else items.push({name:'syubetu5', value: '0'});
		
		items.push({name:'ekitoho', value:eki.indexOf(keyHash['ekitoho'])});
		if(keyHash['bas_riyou'] == 'on') items.push({name:'bas_riyou', value: '1'});
		else items.push({name:'bas_riyou', value: '0'});
		
		//間取り
		for(var i = 0 ; i < madoris.length ; i++){
			if(keyHash['madori' + madoris[i]] == 'on') items.push({name:'madori' + madoris[i], value: '1'});
			else items.push({name:'madori' + madoris[i], value: '0'});
		}
		items.push({name:'madoriGroupCheck', value:'0'});
		
		//builts
		if(keyHash['built'] != undefined){
			items.push({name:'built', value:builts.indexOf(keyHash['built'])});
		}else{
			items.push({name:'built', value:'0'});
		}
		
		//人気こだわり
		for(var i = 1; i <= 9 ; i++){
			var ninki = 'kodawari_0' + String(i);
			if(keyHash[ninki] == 'on') items.push({name:ninki, value: '1'});
			else items.push({name:ninki, value: '0'});
		}
		
		//間取りGroup
		var hasOne = false;
		for(var i = 0 ; i < ko1.length ; i++){
			var maName = 'kodawari' + String(ko1[i]);						
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_01', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_01', value: '0'});
		
		
		hasOne = false;
		for(var i = 0 ; i < ko2.length ; i++){
			var maName = 'kodawari' + String(ko2[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_02', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_02', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko3.length ; i++){
			var maName = 'kodawari' + String(ko3[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_03', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_03', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko4.length ; i++){
			var maName = 'kodawari' + String(ko4[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_04', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_04', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko5.length ; i++){
			var maName = 'kodawari' + String(ko5[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_05', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_05', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko6.length ; i++){
			var maName = 'kodawari' + String(ko6[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_06', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_06', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko7.length ; i++){
			var maName = 'kodawari' + ko7[i];
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_07', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
			
		}
		if(!hasOne)items.push({name:'kodawariType_07', value: '0'});

		
		hasOne = false;
		for(var i = 0 ; i < ko8.length ; i++){
			var maName = 'kodawari' + String(ko8[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_08', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_08', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko9.length ; i++){
			var maName = 'kodawari' + String(ko9[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_09', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_09', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko10.length ; i++){
			var maName = 'kodawari' + String(ko10[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_10', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_10', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko11.length ; i++){
			var maName = 'kodawari' + String(ko11[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_11', value: '1'});
				}								
				items.push({name:maName, value:'1'});
			}
			else {
				items.push({name:maName, value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_11', value: '0'});
		
		//console.dir(items);
		//キャンペイン
		var campaign = [];		
		var cpList = nameList[nameList.length - 2].other;
		if(cpList.indexOf('バルセロナ部屋') >= 0) campaign.push('バルセロナ部屋');
		if(cpList.indexOf('ポンタ部屋') >= 0) campaign.push('ポンタ部屋');
		if(cpList.indexOf('友の会部屋') >= 0) campaign.push('友の会部屋');
		if(cpList.indexOf('家電部屋特集') >= 0) campaign.push('家電部屋特集');
		items.push({name:'campaignName', value:campaign.join('/')});
		//キャンペイン
		//console.dir(items);
		submit(href, 'POST', items);
	}
	/********************************************************
	 * 条件を問い合わせ
	 ********************************************************/
	function inquirySetting(){
		$('.btn_contact a').on('click', function(e){
			var href = $(this).attr('href');
			e.preventDefault();
			saveInquiryCondition(href);
		});
	}
	
	/* 実行
	----------------------------------- */
	$(function() {
		init();
		setSelectBtn();
		checkSelectBtn();
		modalPosition();
		inquirySetting();
	});

	function submit(action, method, values) {
 	   var form = $('<form/>', {
	        action: action,
	        method: method
	    });
	    $.each(values, function() {
	        form.append($('<input/>', {
	            type: 'hidden',
	            name: this.name,
	            value: this.value
	        }));    
	    });
	    form.appendTo('body').submit();
	}
	
	function go(page) {
		var items=[];
		var typ = $('input[name="search_type"]').attr("value");
		items.push({name:'search_type',value:typ});

		$('.search_target').each(function(index){
			items.push({name:'target_'+index,
				    value:$(this).attr("id")});
		});

		$('.mod_checkbox input:checked').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.attr("value"),});
		});

		$('select').each(function(index){
			var elem = $(this);
			items.push({name:elem.attr("name"),
				    value:elem.find("option:selected").attr("value"),});
		});
			
		items.push({name:'page',value:page||1});
		submit('/sp/rstobi/rstobi','POST',items);
	}
	ReSearch = go;
	
	function go2(page) {
		var items=[];
		var form = document.forms[0];
		for (var i=0; i<form.length; i++){
			if (form[i].name == "this_params[page]") {
				// skip
			} else if (form[i].name.substr(0,11) == "this_params") {
				if (form[i].name.match(/^this_params\[(.+)\]\[(.+)\]$/, "$1[$2]")) {
					items.push({name:form[i].name.replace(/^this_params\[(.+)\]\[(.+)\]$/, "$1[$2]"),value:form[i].value});
				} else if (form[i].name.match(/^this_params\[(.+)\]$/, "$1")) {
					items.push({name:form[i].name.replace(/^this_params\[(.+)\]$/, "$1"),value:form[i].value});
				}
			}
		}
		
		items.push({name:'page',value:page||1});
		submit('/sp/rstobi/rstobi','POST',items);
	}
	PageMove = go2;

	$(function () {
		$('.mod_box_area').on('click', function () {
			var len = $('.sec_detail_otherroom .box_expand_inner :checkbox:checked').length;
			if (len > 0 && len <= 20) {
				$('form').submit();
			}
			return false;
		});
	});

})(this, document);